/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jili.ubert.clientapi;

import com.jili.ubert.clientapi.until.UnWorkException;
import com.jili.ubert.clientapi.until.GetVerCode;
import com.jili.ubert.clientapi.until.AnswerHandler;
import com.jili.ubert.clientapi.until.AnswerMsgResult;
import com.jili.ubert.clientapi.until.AbstractWorker;
import com.alibaba.fastjson.JSON;
import com.jili.ubert.clientapi.provider.ManagementProvider;
import com.jili.ubert.clientapi.provider.TradeProvider;
import com.jili.ubert.code.CodeTool;
import com.jili.ubert.dao.db.AlterOrder;
import com.jili.ubert.code.client2server.BasketCancelOrders;
import com.jili.ubert.code.client2server.BasketOrders;
import com.jili.ubert.dao.db.CancelOrder;
import com.jili.ubert.dao.db.FundTransfer;
import com.jili.ubert.code.client2server.Login;
import com.jili.ubert.code.client2server.Logout;
import com.jili.ubert.dao.db.NewOrder;
import com.jili.ubert.code.client2server.ReSetPassWord;
import com.jili.ubert.code.client2server.RequestPageData;
import com.jili.ubert.code.server2client.MsgResult;
import com.jili.ubert.code.server2client.MsgResultList;
import com.panemu.tiwulfx.common.TableData;
import com.jili.ubert.code.server2client.PageDataProto;
import com.jili.ubert.code.server2client.SystemMessage;
import com.jili.ubert.dao.DBOperation;
import com.jili.ubert.dao.DBTableName;
import com.jili.ubert.dao.OnDBDataAccpet;
import com.jili.ubert.dao.db.AccountInfo;
import com.jili.ubert.dao.db.AccountInvest;
import com.jili.ubert.dao.db.FundState;
import com.jili.ubert.dao.db.PosDetail;
import com.jili.ubert.dao.db.OrderExecuteProgress;
import com.jili.ubert.dao.db.TradeReportDetail;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author ChengJili
 */
class TradeClient implements TradeProvider, ManagementProvider{

    private static final Log log = LogFactory.getLog(TradeClient.class);
    private final SessionBucket sessionBucket = new SessionBucket();
    private final ListenFundStateBucket listenFundStateBucket = new ListenFundStateBucket(16);
    private final ListenOrderExecuteProgressBucket listenOrderExecuteProgressBucket = new ListenOrderExecuteProgressBucket();
    private final ListenPOSBucket listenPOSBucket = new ListenPOSBucket(16);
    private final ListenSystemMessageBucket listenSystemMessageBucket = new ListenSystemMessageBucket(4);
    private final ListenCancelOrderBucket listenCancelOrderBucket = new ListenCancelOrderBucket(4);
    private final ListenAlterOrderBucket listenAlterOrderBucket = new ListenAlterOrderBucket(4);
    private final ListenNewOrderBucket listenNewOrderBucket = new ListenNewOrderBucket(4);

    private final HashMap<Integer, OnDBDataAccpet> dbdataacceptor = new HashMap<>();
    //1.成员变量
    private final TradeNettyClient tradeNettyClient;
    private DataPool dataPool;
    private TradeRequest request;
    private AtomicInteger requstid = new AtomicInteger(0);

    private int getRequstID() {
        if (requstid.getAndIncrement() == Integer.MAX_VALUE) {
            requstid.getAndSet(Integer.MIN_VALUE);
        }
        return requstid.intValue();
    }
//    private HashMap<String,List<PriceListener>> hanglersPrice;
//    private HashSet<PriceListener> hanglersPrice;
    //2.构造函数

    public TradeClient() {
        this.tradeNettyClient = new TradeNettyClient();
        log.debug("TradeClient构造完成");
    }

    public TradeClient(ExecutorService exec, String host, int port) {
        this.tradeNettyClient = new TradeNettyClient();
        MsgResult ret = tradeNettyClient.Connect(host, port, false, false, this);
        if (!ret.getSuccess()) {
            throw new UnWorkException(ret.getWord());
        }
    }

//    @Override
    public MsgResult Connect(ExecutorService exec, String host, int port, boolean isSSL, boolean isZlib) {
        log.debug("正在登陆IP:" + host + "  Port:" + port);
        tradeNettyClient.config(host, port, isSSL, isZlib, this);
        exec.execute(tradeNettyClient);
        return tradeNettyClient.getConnectRST();
        //    return tradeNettyClient.Connect(host, port, isSSL, isZlib, this);
    }

//    @Override
    public int Login(Login login, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(1, id, login.EnCode(login));
        return id;
    }

//    @Override
    public int UserLogin(Login login, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(48, id, login.EnCode(login));
        return id;
    }

//    @Override
    public int getVerlidCode(AnswerHandler<String> handler) {
        GetVerCode gvcode = new GetVerCode(handler);
        int id = getRequstID();
        this.sessionBucket.AcceptSession(id, gvcode);
        gvcode.fire(tradeNettyClient, id);
        return id;
    //    TradeClientHanlder handler1 = tradeNettyClient.SendSYNCMsg( 0, getRequstID(), new byte[1]);

        //    ret = handler.getVerlidCode();
    }

//    @Override
    public int Logout(Logout logout, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(2, id, logout.EnCode(logout));
        return id;
    }

    @Override
    public int CheckOrder(NewOrder order, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(13, id, order.EnCode(order));
        return id;
    }

    @Override
    public int CheckOrder(BasketOrders orders, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(14, getRequstID(), orders.EnCode(orders));
        return id;
    }

    @Override
    public int CheckBasketOrder(BasketOrders orders, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(15, id, orders.EnCode(orders));
        return id;
    }

    @Override
    public int CheckOrder(AlterOrder alterOrder, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(16, id, alterOrder.EnCode(alterOrder));
        return id;
    }

    @Override
    public int CheckOrder(CancelOrder cancelOrder, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(17, id, cancelOrder.EnCode(cancelOrder));
        return id;
    }

    @Override
    public int Send(NewOrder order, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(3, id, order.EnCode(order));
        return id;
    }

    @Override
    public int Send(BasketOrders basketOrders, AnswerHandler<MsgResultList> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                MsgResultList data = new MsgResultList();
                try {
                    data = data.DeCode(msg.getData());
                    getAnswerHandler().OnAnswer(data);
                } catch (IOException ex) {
                    data.setReturnID(100);
                    data.setSuccess(false);
                    data.setWord(ex.getMessage());
                    getAnswerHandler().OnAnswer(data);
                }
            }
        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(5, id, basketOrders.EnCode(basketOrders));
        return id;
    }

    @Override
    public int Send(CancelOrder cancelOrder, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(4, id, cancelOrder.EnCode(cancelOrder));
        return id;
    }

    @Override
    public int Send(BasketCancelOrders bCancelorders, AnswerHandler<MsgResultList> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                MsgResultList data = new MsgResultList();
                try {
                    data = data.DeCode(msg.getData());
                    getAnswerHandler().OnAnswer(data);
                } catch (IOException ex) {
                    data.setReturnID(100);
                    data.setSuccess(false);
                    data.setWord(ex.getMessage());
                    getAnswerHandler().OnAnswer(data);
                }
            }
        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(6, id, bCancelorders.EnCode(bCancelorders));
        return id;
    }

    @Override
    public int Send(AlterOrder alterOrder, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(7, id, AlterOrder.EnCode(alterOrder));
        return id;
    }

    @Override
    public int InnerExeSend(NewOrder order, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(18, id, NewOrder.EnCode(order));
        return id;
    }

    @Override
    public int InnerExeSend(CancelOrder cancelOrder, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(19, id, CancelOrder.EnCode(cancelOrder));
        return id;
    }

    @Override
    public int InnerExeSend(AlterOrder alterOrder, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(20, id, AlterOrder.EnCode(alterOrder));
        return id;
    }

    private int fundtransfer(FundTransfer ft, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(21, id, FundTransfer.EnCode(ft));
        return id;
    }
    private int fundtransfer(String accountCode, double amount,String dr,  String remark,AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        FundTransfer ft = new FundTransfer();
        ft.setAccountCode(accountCode);
        ft.setAmount(amount);
        ft.setReMark(remark);
        ft.setDirect(dr);
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(21, id, FundTransfer.EnCode(ft));
        return id;
    }

    @Override
    public int Bank2Account(String accountCode, double amount, String remark, AnswerHandler<MsgResult> handler) throws IOException {
        return fundtransfer(accountCode, amount, "I", remark, handler);
    }

    @Override
    public int Account2Bank(String accountCode, double amount, String remark, AnswerHandler<MsgResult> handler) throws IOException {
        return fundtransfer(accountCode, amount, "O", remark, handler);
    }

    @Override
    public int AddDelta(String accountCode, double amount, String remark, AnswerHandler<MsgResult> handler) throws IOException {
        return fundtransfer(accountCode, amount, "A", remark, handler);
    }

    @Override
    public int CutDelta(String accountCode, double amount, String remark, AnswerHandler<MsgResult> handler) throws IOException {
        return fundtransfer(accountCode, amount, "C", remark, handler);
    }

    @Override
    public int QueryPosDetails(String accountCode, AnswerHandler<TableData<PosDetail>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<PosDetail> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<PosDetail> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(PosDetail.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("getPosDetails-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("getPosDetails fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add(accountCode);
        rqt.setFilteredColumns(acclist);
        tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rqt));
        return id;
    }

    @Override
    public int QueryOrderExecuteProgress(String accountCode, AnswerHandler<TableData<OrderExecuteProgress>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<OrderExecuteProgress> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<OrderExecuteProgress> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(OrderExecuteProgress.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("getOrderExecuteProgress-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("getOrderExecuteProgress fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add(accountCode);
        rqt.setFilteredColumns(acclist);
        tradeNettyClient.SendSYNCMsg(23, id, RequestPageData.EnCode(rqt));
        return id;
    }

    @Override
    public int QueryAccountList(AnswerHandler<TableData<AccountInfo>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<AccountInfo> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<AccountInfo> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(AccountInfo.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryAccountList-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryAccountList fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add("all");
        rqt.setFilteredColumns(acclist);
        tradeNettyClient.SendSYNCMsg(24, id, RequestPageData.EnCode(rqt));
        return id;
    }

    @Override
    public int QueryFundStatus(AnswerHandler<TableData<FundState>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<FundState> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<FundState> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(FundState.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryAccountList-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryAccountList fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add("all");
        rqt.setFilteredColumns(acclist);
        tradeNettyClient.SendSYNCMsg(25, id, RequestPageData.EnCode(rqt));
        return id;
    }

    @Override
    public int QueryFundStatus(String accountCode, AnswerHandler<TableData<FundState>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<FundState> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<FundState> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(FundState.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("getOrderExecuteProgress-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("getOrderExecuteProgress fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add(accountCode);
        rqt.setFilteredColumns(acclist);
        tradeNettyClient.SendSYNCMsg(26, id, RequestPageData.EnCode(rqt));
        return id;
    }

    @Override
    public int ResetUserPassWord(String userID, String passWord, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        ReSetPassWord rsp = new ReSetPassWord(userID, passWord);
        tradeNettyClient.SendSYNCMsg(27, id, rsp.EnCode(rsp));
        return id;
    }

    @Override
    public int AddInvestID(String investID, String brokorID, AnswerHandler<MsgResult> handler) throws IOException {
        return AddInvestID(investID, "", brokorID, handler);
    }

    @Override
    public int AddInvestID(String investID, String passWord, String brokorID, AnswerHandler<MsgResult> handler) throws IOException {
        AccountInvest invest = new AccountInvest();
        invest.setBrokerID(brokorID);
        invest.setInvestID(investID);
        invest.setPwd(passWord);
        return AddInvestID(invest,handler);
    }
    @Override
    public int AddInvestID(AccountInvest invest, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(28, id, AccountInvest.EnCode(invest));
        return id;
    }

    @Override
    public int DelInvestID(String investID, String brokorID, AnswerHandler<MsgResult> handler) throws IOException {
        AccountInvest invest = new AccountInvest();
        invest.setBrokerID(brokorID);
        invest.setInvestID(investID);
        return DelInvestID(invest,handler);
    }
    @Override
    public int DelInvestID(AccountInvest invest, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(29, id, AccountInvest.EnCode(invest));
        return id;
    }

    @Override
    public int CreateAccountCode(String name, String type, AnswerHandler<MsgResult> handler) throws IOException {
        AccountInfo acc = new AccountInfo();
        acc.setAccountName(name);
        acc.setType(type);
        return CreateAccountCode(acc,handler);
    }
    @Override
    public int CreateAccountCode(AccountInfo acc, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(30, id, AccountInfo.EnCode(acc));
        return id;
    }

    @Override
    public int AlterAccountCode(AccountInfo acc, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(31, id, AccountInfo.EnCode(acc));
        return id;
    }

    @Override
    public int getOrderExeLastStatus(int orderID, AnswerHandler<OrderExecuteProgress> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {
            @Override
            public void Onhandler(MsgEntity msg) {
                try {
                    OrderExecuteProgress data;
                    data = OrderExecuteProgress.DeCode(msg.getData());
                    getAnswerHandler().OnAnswer(data);
                } catch (IOException ex) {
                    log.info("getOrderExeLastStatus fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(32, id, CodeTool.IntToBit(orderID));
        return id;
    }

    @Override
    public int getOrderReportDetail(int orderID, AnswerHandler<List<FundState>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data = new PageDataProto();
                try {
                    data = data.DeCode(msg.getData());
                    List<TradeReportDetail> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(TradeReportDetail.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("getOrderReportDetail-PageDataProto fail decode:" + ex);
                        }
                    });
                    getAnswerHandler().OnAnswer(poss);
                } catch (IOException ex) {
                    log.info("getOrderReportDetail fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        tradeNettyClient.SendSYNCMsg(33, id, CodeTool.IntToBit(orderID));
        return id;
    }
    @Override
    public int RemoveFundFlushListener(String accountCode, AnswerHandler<FundState> handler) throws IOException {
        int id = getRequstID();
        this.listenFundStateBucket.CancelRegister(accountCode, handler);
        tradeNettyClient.SendSYNCMsg(35, id, CodeTool.StringToBit1(accountCode));
        return id;
    }

    @Override
    public int RegistFundFlush(String accountCode, AnswerHandler<FundState> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {
            @Override
            public void Onhandler(MsgEntity msg) {
                try {
                    MsgResult data;
                    data = MsgResult.DeCode(msg.getData());
                    if (data.isSuccess()) {
                        getAnswerHandler().IsSuccess();
                    } else {
                        getAnswerHandler().OnAnswer(data);
                    }

                } catch (IOException ex) {
                    log.info("getOrderExeLastStatus fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.listenFundStateBucket.Register(accountCode, handler);
        tradeNettyClient.SendMsg(34, id, CodeTool.StringToBit1(accountCode));
        return id;
    }

    @Override
    public int RegistPosFlush(String accountCode, AnswerHandler<PosDetail> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {
            @Override
            public void Onhandler(MsgEntity msg) {
                try {
                    MsgResult data;
                    data = MsgResult.DeCode(msg.getData());
                    if (data.isSuccess()) {
                        getAnswerHandler().IsSuccess();
                    } else {
                        getAnswerHandler().OnAnswer(data);
                    }

                } catch (IOException ex) {
                    log.info("getOrderExeLastStatus fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.listenPOSBucket.Register(accountCode, handler);
        tradeNettyClient.SendMsg(36, id, CodeTool.StringToBit1(accountCode));
        return id;
    }

    @Override
    public int RemovPosFlushListener(String accountCode, AnswerHandler<PosDetail> handler) throws IOException {
        int id = getRequstID();
        this.listenPOSBucket.CancelRegister(accountCode, handler);
        tradeNettyClient.SendSYNCMsg(37, id, CodeTool.StringToBit1(accountCode));
        return id;
    }

    @Override
    public void RegistNewOrders(String accountCode, AnswerHandler<NewOrder> handler) throws IOException{
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {
            @Override
            public void Onhandler(MsgEntity msg) {
                try {
                    MsgResult data;
                    data = MsgResult.DeCode(msg.getData());
                    if (data.isSuccess()) {
                        getAnswerHandler().IsSuccess();
                    } else {
                        getAnswerHandler().OnAnswer(data);
                    }

                } catch (IOException ex) {
                    log.info("RemoveCancelOrdersListener fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.listenNewOrderBucket.Register(accountCode, handler);
        tradeNettyClient.SendMsg(38, getRequstID(), CodeTool.StringToBit1(accountCode));
    }

    @Override
    public int RemoveNewOrdersListener(String accountCode, AnswerHandler<NewOrder> handler) throws IOException {
        int id = getRequstID();
        this.listenNewOrderBucket.CancelRegister(accountCode, handler);
        tradeNettyClient.SendSYNCMsg(39, getRequstID(), CodeTool.StringToBit1(accountCode));
        return id;
    }

    @Override
    public void RegistCancelOrders(String accountCode, AnswerHandler<CancelOrder> handler) throws IOException{
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {
            @Override
            public void Onhandler(MsgEntity msg) {
                try {
                    MsgResult data;
                    data = MsgResult.DeCode(msg.getData());
                    if (data.isSuccess()) {
                        getAnswerHandler().IsSuccess();
                    } else {
                        getAnswerHandler().OnAnswer(data);
                    }

                } catch (IOException ex) {
                    log.info("RemoveCancelOrdersListener fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.listenCancelOrderBucket.Register(accountCode, handler);
        tradeNettyClient.SendMsg(40, id, CodeTool.StringToBit1(accountCode));
    }

    @Override
    public int RemoveCancelOrdersListener(String accountCode, AnswerHandler<CancelOrder> handler) throws IOException {
        int id = getRequstID();
        this.listenCancelOrderBucket.CancelRegister(accountCode, handler);
        tradeNettyClient.SendSYNCMsg(41, id, CodeTool.StringToBit1(accountCode));
        return id;
    }

    @Override
    public void RegistAlterOrders(String accountCode, AnswerHandler<AlterOrder> handler) throws IOException{
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {
            @Override
            public void Onhandler(MsgEntity msg) {
                try {
                    MsgResult data;
                    data = MsgResult.DeCode(msg.getData());
                    if (data.isSuccess()) {
                        getAnswerHandler().IsSuccess();
                    } else {
                        getAnswerHandler().OnAnswer(data);
                    }

                } catch (IOException ex) {
                    log.info("getOrderExeLastStatus fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.listenAlterOrderBucket.Register(accountCode, handler);
        tradeNettyClient.SendMsg(42, id, CodeTool.StringToBit1(accountCode));
    }

    @Override
    public int RemoveAlterOrdersListener(String accountCode, AnswerHandler<AlterOrder> handler) throws IOException {
        int id = getRequstID();
        this.listenAlterOrderBucket.CancelRegister(accountCode, handler);
        tradeNettyClient.SendSYNCMsg(43, id, CodeTool.StringToBit1(accountCode));
        return id;
    }

    @Override
    public void RegistOrderExecuteProgress(String accountCode, AnswerHandler<OrderExecuteProgress> handler) throws IOException{
        tradeNettyClient.SendMsg(44, getRequstID(), CodeTool.StringToBit1(accountCode));
    }

    @Override
    public int RemoveOrderExecuteProgressListener(String accountCode, AnswerHandler<OrderExecuteProgress> handler) throws IOException {
        int id = getRequstID();
        this.listenOrderExecuteProgressBucket.CancelRegister(accountCode, handler);
        tradeNettyClient.SendSYNCMsg(45, id, CodeTool.StringToBit1(accountCode));
        return id;
    }

    @Override
    public int Save(DBTableName tablename, Object obj, AnswerHandler<MsgResult> handler) throws IOException {
        int id = this.getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(tablename);
        dao.setOperation(DBOperation.SAVE);
        dao.setData(CodeTool.StringToBit1(JSON.toJSONString(obj)));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int HSQLSave(String hsql, AnswerHandler<MsgResult> handler) throws IOException {
        int id = this.getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(DBTableName.HSQL);
        dao.setOperation(DBOperation.SAVE);
        dao.setData(CodeTool.StringToBit1(hsql));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int SQLSave(String sql, AnswerHandler<MsgResult> handler) throws IOException {
        int id = this.getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(DBTableName.SQL);
        dao.setOperation(DBOperation.SAVE);
        dao.setData(CodeTool.StringToBit1(sql));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int Update(DBTableName tablename, Object obj, AnswerHandler<MsgResult> handler) throws IOException {
        int id = this.getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(tablename);
        dao.setOperation(DBOperation.UPDATE);
        dao.setData(CodeTool.StringToBit1(JSON.toJSONString(obj)));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int HSQLUpdate(String hsql, AnswerHandler<MsgResult> handler) throws IOException {
        int id = this.getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(DBTableName.HSQL);
        dao.setOperation(DBOperation.UPDATE);
        dao.setData(CodeTool.StringToBit1(hsql));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int SQLUpdate(String sql, AnswerHandler<MsgResult> handler) throws IOException {
        int id = this.getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(DBTableName.SQL);
        dao.setOperation(DBOperation.UPDATE);
        dao.setData(CodeTool.StringToBit1(sql));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int Delete(DBTableName tablename, Object obj, AnswerHandler<MsgResult> handler) throws IOException {
        int id = this.getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(tablename);
        dao.setOperation(DBOperation.DELETE);
        dao.setData(CodeTool.StringToBit1(JSON.toJSONString(obj)));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int HSQLDelete(String hsql, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(DBTableName.HSQL);
        dao.setOperation(DBOperation.DELETE);
        dao.setData(CodeTool.StringToBit1(hsql));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int SQLDelete(String sql, AnswerHandler<MsgResult> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AnswerMsgResult();
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(DBTableName.SQL);
        dao.setOperation(DBOperation.DELETE);
        dao.setData(CodeTool.StringToBit1(sql));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int Select(DBTableName tablename, String key, AnswerHandler<TableData> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                TableData data = new TableData();
                String str = CodeTool.BitToString(msg.getData());
                //data = data.Decode(str);
                getAnswerHandler().OnAnswer(data);
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(tablename);
        dao.setOperation(DBOperation.SELECT);
        dao.setData(CodeTool.StringToBit1(key));
        tradeNettyClient.SendSYNCMsg(1000, id, DAOEntity.EnCode(dao));
        return id;
    }

    @Override
    public int HSQLSelect(String hsql, AnswerHandler<TableData> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                TableData data = new TableData();
                String str = CodeTool.BitToString(msg.getData());
                //data = data.Decode(str);
                getAnswerHandler().OnAnswer(data);
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(DBTableName.SQL);
        dao.setOperation(DBOperation.SELECT);
        dao.setData(CodeTool.StringToBit1(hsql));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    @Override
    public int SQLSelect(String sql, AnswerHandler<TableData> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                TableData data = new TableData();
                String str = CodeTool.BitToString(msg.getData());
                //data = data.Decode(str);
                getAnswerHandler().OnAnswer(data);
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        DAOEntity dao = new DAOEntity();
        dao.setTableName(DBTableName.SQL);
        dao.setOperation(DBOperation.SELECT);
        dao.setData(CodeTool.StringToBit1(sql));
        this.tradeNettyClient.SendSYNCMsg(1000, id, dao.EnCode(dao));
        return id;
    }

    public void AccpetDBData(int reqid, DAOEntity data) {

    }

    public void AccpetSessionAnswer(int id,MsgEntity msg) {
        this.sessionBucket.OnDealSession(id,msg);
    }
    public void AccpetSessionFaileAnswer(int id,MsgResult msg) {
        this.sessionBucket.OnFaile(id, msg);
    }

    public void OnPosDetail(PosDetail pos) {
        this.listenPOSBucket.OnDealListen(pos);
    }

    public void OnFundState(FundState fund) {
        this.listenFundStateBucket.OnDealListen(fund);
    }

    public void OnOrderExecuteProgress(OrderExecuteProgress orderexe) {
        this.listenOrderExecuteProgressBucket.OnDealListen(orderexe);
    }

    public void OnSystemMessage(SystemMessage msg) {
        this.listenSystemMessageBucket.OnDealListen(msg);
    }
    public void OnNewOrder(NewOrder msg) {
        this.listenNewOrderBucket.OnDealListen(msg);
    }
    public void OnCancelOrder(CancelOrder msg) {
        this.listenCancelOrderBucket.OnDealListen(msg);
    }
    public void OnAlterOrder(AlterOrder msg) {
        this.listenAlterOrderBucket.OnDealListen(msg);
    }
    
    @Override
    public int QueryAccountList(RequestPageData rpd, AnswerHandler<TableData<AccountInfo>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<AccountInfo> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<AccountInfo> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(AccountInfo.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryAccountList-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryAccountList fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rpd));
        return id;
    }

    @Override
    public int QueryFundStatus(RequestPageData rpd, AnswerHandler<TableData<FundState>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<FundState> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<FundState> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(FundState.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryFundStatus-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryFundStatus fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rpd));
        return id;
    }

    @Override
    public int RegistFundFlush(AnswerHandler<FundState> handler) throws IOException {
        return RegistFundFlush("all",handler);
    }

    @Override
    public int RemoveFundFlushListener(AnswerHandler<FundState> handler) throws IOException {
        int id = getRequstID();
        this.listenFundStateBucket.CancelRegister(handler);
        return id;
        
    }

    @Override
    public int QueryPosDetails(RequestPageData rpd, AnswerHandler<TableData<PosDetail>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<PosDetail> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<PosDetail> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(PosDetail.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryPosDetails-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryPosDetails fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rpd));
        return id;
    }

    @Override
    public int RegistPosFlush(AnswerHandler<PosDetail> handler) throws IOException {
        return RegistPosFlush("all",handler);
    }

    @Override
    public int RemovPosFlushListener(AnswerHandler<PosDetail> handler) throws IOException {
        int id = getRequstID();
        this.listenPOSBucket.CancelRegister(handler);
        return id;
    }

    @Override
    public int QueryOrderExecuteProgress(RequestPageData rpd, AnswerHandler<TableData<OrderExecuteProgress>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<OrderExecuteProgress> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<OrderExecuteProgress> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(OrderExecuteProgress.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryOrderExecuteProgress-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryOrderExecuteProgress fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rpd));
        return id;
    }

    @Override
    public void RegistOrderExecuteProgress(AnswerHandler<OrderExecuteProgress> handler) throws IOException {
        RegistOrderExecuteProgress("all",handler);
    }

    @Override
    public int RemoveOrderExecuteProgressListener(AnswerHandler<OrderExecuteProgress> handler) throws IOException {
        int id = getRequstID();
        this.listenOrderExecuteProgressBucket.CancelRegister(handler);
        return id;
    }

    @Override
    public void QueryNewOrders(String accountCode, AnswerHandler<TableData<NewOrder>> handler) throws IOException {
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add(accountCode);
        rqt.setFilteredColumns(acclist);
        QueryNewOrders(rqt,handler);
    }

    @Override
    public void QueryNewOrders(RequestPageData rpd, AnswerHandler<TableData<NewOrder>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<NewOrder> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<NewOrder> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(NewOrder.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryNewOrders-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryNewOrders fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rpd));
    }

    @Override
    public void RegistNewOrders(AnswerHandler<NewOrder> handler) throws IOException {
        RegistNewOrders("all",handler);
    }

    @Override
    public int RemoveNewOrdersListener(AnswerHandler<NewOrder> handler) throws IOException {
        int id = getRequstID();
        this.listenNewOrderBucket.CancelRegister(handler);
        return id;
    }

    @Override
    public void QueryCancelOrders(String accountCode, AnswerHandler<TableData<CancelOrder>> handler) throws IOException {
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add(accountCode);
        rqt.setFilteredColumns(acclist);
        QueryCancelOrders(rqt,handler);
    }

    @Override
    public void QueryCancelOrders(RequestPageData rpd, AnswerHandler<TableData<CancelOrder>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<CancelOrder> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<CancelOrder> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(CancelOrder.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryCancelOrders-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryCancelOrders fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rpd));

    }

    @Override
    public void RegistCancelOrders(AnswerHandler<CancelOrder> handler) throws IOException {
        RegistCancelOrders("all",handler);
    }

    @Override
    public int RemoveCancelOrdersListener(AnswerHandler<CancelOrder> handler) throws IOException {
        int id = getRequstID();
        this.listenCancelOrderBucket.CancelRegister(handler);
        return id;
    }

    @Override
    public void QueryAlterOrders(String accountCode, AnswerHandler<TableData<AlterOrder>> handler) throws IOException {
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add(accountCode);
        rqt.setFilteredColumns(acclist);
        QueryAlterOrders(rqt,handler);
    }

    @Override
    public void QueryAlterOrders(RequestPageData rpd, AnswerHandler<TableData<AlterOrder>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<AlterOrder> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<AlterOrder> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(AlterOrder.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryAlterOrders-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryAlterOrders fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rpd));
    }

    @Override
    public void RegistAlterOrders(AnswerHandler<AlterOrder> handler) throws IOException {
        RegistAlterOrders("all",handler);
    }

    @Override
    public int RemoveAlterOrdersListener(AnswerHandler<AlterOrder> handler) throws IOException {
        int id = getRequstID();
        this.listenAlterOrderBucket.CancelRegister(handler);
        return id;
    }

    @Override
    public void QueryOrderReports(int orderID, AnswerHandler<TableData<TradeReportDetail>> handler) throws IOException {
        RequestPageData rqt = new RequestPageData();
        rqt.setStartIndex(1);
        rqt.setMaxResult(500);
        List<String> acclist = new ArrayList<>();
        acclist.add(String.valueOf(orderID));
        rqt.setFilteredColumns(acclist);
        QueryOrderReports(rqt,handler);
    }

    @Override
    public void QueryOrderReports(RequestPageData rpd, AnswerHandler<TableData<TradeReportDetail>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<TradeReportDetail> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<TradeReportDetail> poss = new ArrayList<>();
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(TradeReportDetail.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryOrderReports-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryOrderReports fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(22, id, RequestPageData.EnCode(rpd));
    }
    public int QueryOrderExecuteProgressList(RequestPageData rpd, AnswerHandler<TableData<OrderExecuteProgress>> handler) throws IOException {
        int id = getRequstID();
        AbstractWorker worker = new AbstractWorker() {

            @Override
            public void Onhandler(MsgEntity msg) {
                PageDataProto data;
                try {
                    data = PageDataProto.DeCode(msg.getData());
                    TableData<OrderExecuteProgress> data0 = new TableData<>();
                    //data0.setCurrentPage(data.getCurrentPage());
                    //data0.setMaxResult(data.getMaxNum());
                    data0.setTotalRows(data.getTotalRows());
                    List<OrderExecuteProgress> poss = new ArrayList<>(data.getMaxNum());
                    data.getDataList().stream().forEach((byte[] posb) -> {
                        try {
                            poss.add(OrderExecuteProgress.DeCode(posb));
                        } catch (IOException ex) {
                            log.info("QueryOrderExecuteProgressList-PageDataProto fail decode:" + ex);
                        }
                    });
                    data0.setRows(poss);
                    getAnswerHandler().OnAnswer(data0);
                } catch (IOException ex) {
                    log.info("QueryOrderExecuteProgressList fail decode:" + ex);
                }
            }

        };
        worker.setAnswerHandler(handler);
        this.sessionBucket.AcceptSession(id, worker);
        this.tradeNettyClient.SendSYNCMsg(537, id, RequestPageData.EnCode(rpd));
        return id;
    }

    public void setDataPool(DataPool datapool) {
        this.dataPool = datapool;
    }

    public void setRequest(TradeRequest tradeRequest) {
        this.request = tradeRequest;
    }
}
